package com.joysuch.wwyt.dzhapi.controller;

import com.joysuch.wwyt.dzhapi.dto.BuildDataDto;
import com.joysuch.wwyt.dzhapi.exception.ApiException;
import com.joysuch.wwyt.dzhapi.req.ApiBaseUserAddReq;
import com.joysuch.wwyt.dzhapi.req.ApiBaseUserUpdateReq;
import com.joysuch.wwyt.dzhapi.req.ApiBaseUserUpdateWorkCompanyReq;
import com.joysuch.wwyt.dzhapi.req.ApiDataReq;
import com.joysuch.wwyt.dzhapi.result.ApiResult;
import com.joysuch.wwyt.dzhapi.service.ApiBaseUserService;
import com.joysuch.wwyt.dzhapi.service.ApiTokenVerifyService;
import com.joysuch.wwyt.dzhapi.vo.ApiResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author: JamesZY
 * @date: 2021/9/16
 * @description:人员api
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ApiBaseUserController {
    private final ApiBaseUserService service;
    private final ApiTokenVerifyService verifyService;

    /**
     * 批量删除员工
     */
    @PostMapping("/batch-delete")
    public ApiResult deleteUsers(@RequestBody @Valid ApiDataReq<String> req,
                                 HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiException e) {
            return ApiResult.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> dataList = service.batchDelete(req.getData(),buildDataDto);
        return ApiResult.success(dataList);
    }

    /**
     * 修改员工
     */
    @PostMapping("/batch-update")
    public ApiResult updateUser(@RequestBody @Valid ApiDataReq<ApiBaseUserUpdateReq> req,
                                HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiException e) {
            return ApiResult.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> apiResultData = service.batchUpdate(req.getData(),buildDataDto);
        return ApiResult.success(apiResultData);
    }

    /**
     * 批量新增员工
     */
    @PostMapping("/batch-add")
    public ApiResult batchAdd(@RequestBody @Valid ApiDataReq<ApiBaseUserAddReq> req,
                              HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiException e) {
            return ApiResult.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> apiResultData = service.batchAdd(req.getData(),buildDataDto);
        return ApiResult.success(apiResultData);
    }

    /**
     * 批量修改工作单位
     */
    @PostMapping("/batch-update-wc")
    public ApiResult updateUserWorkCompany(@RequestBody @Valid ApiDataReq<ApiBaseUserUpdateWorkCompanyReq> req,
                                           HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiException e) {
            return ApiResult.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> apiResultData = service.batchUpdateWorkCompany(req.getData(),buildDataDto);
        return ApiResult.success(apiResultData);
    }
}
